package com.tian.scroll.gesture.fragment;

import android.os.Bundle;

import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;

import android.util.Log;
import android.view.GestureDetector;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Scroller;

import com.tian.scroll.R;
import com.tian.scroll.databinding.FragmentSingleGestureBinding;

/**
 * @author tiansongtao, Email:769001510@qq.com, created on 2021/1/19 15:06
 * <p>
 * 单手势识别，主要有点击，长点击，双击，滑动
 */
public class SingleGestureFragment extends Fragment {

    private static final String TAG = "SingleGestureFragment";

    private FragmentSingleGestureBinding b;

    private GestureDetector gestureDetector;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        b = DataBindingUtil.inflate(inflater, R.layout.fragment_single_gesture, container, false);
        initSingleGesture();
        b.mbtSingleGesture.setOnTouchListener((v, event) -> gestureDetector.onTouchEvent(event));
        return b.getRoot();
    }

    private void initSingleGesture() {
        gestureDetector = new GestureDetector(requireActivity(), new GestureDetector.SimpleOnGestureListener() {

            //按下时触发
            @Override
            public boolean onDown(MotionEvent e) {
                Log.i(TAG, "onDown: ");
                return true;
            }

            //按下onDown很短时间没有离开触发，单纯快速点击不会触发，一般不使用该方法。
            @Override
            public void onShowPress(MotionEvent e) {
                Log.i(TAG, "onShowPress: ");
            }

            //触摸抬起时触发，如果只是单纯的单击很快执行顺序为onDown-->onSingleTapUp,长按不会触发该事件
            @Override
            public boolean onSingleTapUp(MotionEvent e) {
                Log.i(TAG, "onSingleTapUp: ");
                return false;
            }

            //滑动触发
            @Override
            public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
                Log.i(TAG, "onScroll: e1=" + e1.getAction() + " e2=" + e2.getAction() + " dx=" + distanceX + " dy=" + distanceY);
                b.mbtSingleGesture.scrollBy((int) distanceX, (int) distanceY);
                return false;
            }

            //快速滑动后的Fling滑动， velocityX表示x方向速度
            @Override
            public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
                Log.i(TAG, "onFling:  e1=" + e1.getAction()  + " e2=" + e2.getAction() + " vx=" + velocityX + " vy=" + velocityY);
                return false;
            }

            //长按触发，长按的触发顺序为onDown-->onShowPress->onLongPress
            @Override
            public void onLongPress(MotionEvent e) {
                Log.i(TAG, "onLongPress: ");
            }


            //单击确认触发，发生在onSingleTapUp之后，抬起后才能确认是否是单击
            @Override
            public boolean onSingleTapConfirmed(MotionEvent e) {
                Log.i(TAG, "onSingleTapConfirmed: ");
                return false;
            }

            //双击确认触发，
            @Override
            public boolean onDoubleTap(MotionEvent e) {
                Log.i(TAG, "onDoubleTap: ");
                return false;
            }

            //双击过程中发生的事件，一般不用
            @Override
            public boolean onDoubleTapEvent(MotionEvent e) {
                Log.i(TAG, "onDoubleTapEvent: e = " + e.getAction());
                return false;
            }

        });
    }
}